﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace UbbTimetable2
{
    /// <summary>
    /// A struct to store classroom data.
    /// </summary>
    public struct Classroom
    {
        int _crId;
        String _crName;
        int _buildingId;
        int _crCapacity;
        String _buildingAddress;
        String _buildingTel;

        public String BuildingTel
        {
            get { return _buildingTel; }
            set { _buildingTel = value; }
        }

        public String BuildingAddress
        {
            get { return _buildingAddress; }
            set { _buildingAddress = value; }
        }

        public int CrCapacity
        {
            get { return _crCapacity; }
            set { _crCapacity = value; }
        }

        public int BuildingId
        {
            get { return _buildingId; }
            set { _buildingId = value; }
        }

        public String CrName
        {
            get { return _crName; }
            set { _crName = value; }
        }

        public int CrId
        {
            get { return _crId; }
            set { _crId = value; }
        }
    }

    /// <summary>
    /// A DAO that handles all data related to classrooms.
    /// </summary>
    public class ClassroomDao : Dao
    {
        private String err = "";

        /// <summary>
        /// The last error message.
        /// </summary>
        public String Err
        {
            get { return err; }
        }

        /// <summary>
        /// Returns the list of classrooms in a building.
        /// </summary>
        /// <param name="buildingId">The id of the building or 0 for any classrooms.</param>
        /// <returns>The list of classrooms in a building.</returns>
        public List<Classroom> GetClassroomList(int buildingId)
        {
            String query = "SELECT * FROM Terem AS T, Epulet AS E WHERE T.EpuletId = E.ID";
            if (buildingId > 0)
            {
                query += " AND EpuletId = " + buildingId.ToString();
            }

            SqlDataReader rdr = ExecuteReader(query, ref err);

            List<Classroom> list = new List<Classroom>();
            if (err == "OK")
            {
                while (rdr.Read())
                {
                    Classroom item = new Classroom();
                    item.CrId = Convert.ToInt32(rdr[0]);
                    item.CrName = rdr[1].ToString().TrimEnd();
                    item.BuildingId = Convert.ToInt32(rdr[2]);
                    item.CrCapacity = Convert.ToInt32(rdr[3]);
                    item.BuildingAddress = rdr[5].ToString().TrimEnd();
                    item.BuildingTel = rdr[6].ToString().TrimEnd();

                    list.Add(item);
                }
            }
            CloseDataReader(rdr);
            return list;
        }
    }
}
